//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：ToolBuildAssetsPackage
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
#if(UNITY_EDITOR)
using System;
using UnityEditor;
using YooAsset.Editor;
namespace HDMCode.Hotfix
{
    public class ToolBuildAssetsPackage : ToolBuildBase
    {
        private string PackageName;
        private EBuildMode EBuildMode;
        private string vesion;

        public ToolBuildAssetsPackage(string PackageName, EBuildMode EBuildMode, bool OutPutStreamingAssets) : base(PackageName, OutPutStreamingAssets)
        {
            this.PackageName = PackageName;
            this.EBuildMode = EBuildMode;
            vesion = GetDefaultPackageVersion();
        }

        /// <summary>
        /// 构建资源包
        /// </summary>
        public void BuildAssetsPackge(Action SuccessCallBack)
        {
            ClearFolder();
            var buildTarget = EditorUserBuildSettings.activeBuildTarget;
            var buildinFileCopyOption = AssetBundleBuilderSetting.GetPackageBuildinFileCopyOption(PackageName, EBuildPipeline.BuiltinBuildPipeline);
            var buildinFileCopyParams = AssetBundleBuilderSetting.GetPackageBuildinFileCopyParams(PackageName, EBuildPipeline.BuiltinBuildPipeline);
            BuiltinBuildParameters buildParameters = new BuiltinBuildParameters();
            buildParameters.BuildOutputRoot = AssetBundleBuilderHelper.GetDefaultBuildOutputRoot();
            buildParameters.BuildinFileRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot();
            buildParameters.BuildPipeline = EBuildPipeline.BuiltinBuildPipeline.ToString();
            buildParameters.BuildTarget = buildTarget;
            buildParameters.BuildMode = EBuildMode;
            buildParameters.PackageName = PackageName;
            buildParameters.PackageVersion = vesion;
            buildParameters.EnableSharePackRule = true;
            buildParameters.VerifyBuildingResult = true;
            buildParameters.FileNameStyle = EFileNameStyle.HashName;
            buildParameters.BuildinFileCopyOption = buildinFileCopyOption;
            buildParameters.BuildinFileCopyParams = buildinFileCopyParams;
            buildParameters.EncryptionServices = CreateEncryptionInstance(PackageName, EBuildPipeline.BuiltinBuildPipeline);
            buildParameters.CompressOption = ECompressOption.LZ4;
            BuiltinBuildPipeline pipeline = new BuiltinBuildPipeline();
            var buildResult = pipeline.Run(buildParameters, true);
            if (buildResult.Success)
            {
                CopyFile();
                SuccessCallBack?.Invoke();
            }
            else
            {
                UnityEngine.Debug.Log($"{PackageName}  包体构建失败！");
            }
        }
    }
}
#endif